home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 1.iso
/
DEMON
/
RISCOS2
/
TCP_131S.ARC
/
c
/
ttydriv
< prev
next >
Wrap
Text File
|
1993-12-22
|
6KB
|
175 lines
#include <stdio.h>
#include <ctype.h>
#include "global.h"
#include "misc.h"
/* TTY input driver */
int ttymode;
#define TTY_COOKED 0
#define TTY_RAW 1
int ttyecho = 1;
#define TTY_NOECHO 0
#define TTY_ECHO 1
#define LINESIZE 256
#define ESC 29
#define CTLR 18
#define CTLW 23
#define F3 0x183
void raw(void)
{
ttymode = TTY_RAW;
}
void cooked(void)
{
ttymode = TTY_COOKED;
}
void echo(void)
{
ttyecho = TTY_ECHO;
}
void noecho(void)
{
ttyecho = TTY_NOECHO;
}
/* Accept characters from the incoming tty buffer and process them
* (if in cooked mode) or just pass them directly (if in raw mode).
* Returns the number of characters available for use; if non-zero,
* also stashes a pointer to the character(s) in the "buf" argument.
*/
/*Control-R added by df for retype of lines - useful in Telnet */
/*Then df got impatient and added Control-W for erasing words */
/*Then g1emm got even more impatient and added F-3 line recall.*/
int ttydriv(int c, char **buf)
{
static char linebuf[LINESIZE];
static char *cp = linebuf;
char *rp ;
static int tty_done_init = 0;
int cnt;
int seenprint ;
if(buf == (char **)NULL)
return 0; /* paranoia check */
if(!tty_done_init)
{
for(cp = linebuf; cp < &linebuf[LINESIZE]; *cp++ = '\r');
cp = linebuf;
tty_done_init = 1;
}
cnt = 0;
switch(ttymode)
{
case TTY_RAW:
if (c < 128)
{
*cp++ = c;
cnt = cp - linebuf;
cp = linebuf;
}
break;
case TTY_COOKED:
/* Perform cooked-mode line editing */
switch(c){
case '\r': /* CR and LF are equivalent */
case '\n':
*cp++ = '\r';
*cp++ = '\n';
cwprintf(NULL, "\r\n");
cnt = cp - linebuf;
cp = linebuf;
break;
case '\b': /* Backspace */
case 0x7F: /* Delete */
if(cp != linebuf)
{
cp--;
if (ttyecho)
cwputs(NULL, "\b \b");
}
break;
case CTLR: /* print line buffer */
if (ttyecho)
cwprintf(NULL, "^R");
cwputs(NULL, "\r\n");
if (ttyecho) {
rp = linebuf ;
while (rp < cp)
cwputchar(NULL, *rp++);
}
break ;
case F3: /* Repeat last line */
while (*cp != '\n' && *cp != '\r' && cp < &linebuf[LINESIZE])
{
if (*cp == 0)
*cp = ' ' ;
if (ttyecho)
cwputchar(NULL, *cp++) ;
}
if(cp != linebuf && *cp == '\n')
{
cp--;
*cp = '\r';
if (ttyecho)
cwputchar(NULL, '\b'); /* to allow for one trailing space */
}
break ;
case ESC: /* Line kill */
if (ttyecho) {
while(cp != linebuf)
{
cp--;
cwputchar(NULL, '\b');
}
} else
cp = linebuf;
break;
case CTLW: /* erase word */
seenprint = 0 ; /* we haven't seen a printable char yet */
while (cp != linebuf)
{
cp--;
cwputchar(NULL, '\b') ;
if (isspace(*cp))
{
if (seenprint)
break ;
}
else
seenprint = 1 ;
}
break ;
default: /* Ordinary character */
*cp++ = c;
if (ttyecho)
cwputchar(NULL, c);
if(cp >= &linebuf[LINESIZE]){
cnt = cp - linebuf;
cp = linebuf;
}
break;
}
}
if(cnt != 0)
{
*buf = linebuf;
for(rp = &linebuf[cnt]; rp < &linebuf[LINESIZE]; *rp++ = '\r');
}
else
*buf = NULLCHAR;
return cnt;
}